/*
 * 	<pre>
 *	EasyPeas.
 *  Copyright (C) 2007 S Owen
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Jul 24, 2007
 * InvocationFactory.java
 * </pre>
 */
package org.easypeas.invocation;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.easypeas.EasyPeasConstants;
import org.easypeas.exceptions.InvokerNotFoundException;
import org.easypeas.objectcreation.ObjectCreatorFactory;

/**
 * Factory class that returns an Invoker implementation.<br>
 * The current invoker is only InvokerImpl, but this factory is here to support
 * the ability for future customised Invokers.
 *
 *
 * @see Invoker
 * @see InvokerImpl
 *
 * @author S Owen
 */
public class InvocationFactory {
    
    private static Logger logger = Logger.getLogger("org.easypeas.invocation.InvocationFactory");
    
    private static InvocationFactory instance = new InvocationFactory();
    
    /**
     * Gets the single instance of InvocationFactory.
     *
     * @return single instance of InvocationFactory
     */
    public static InvocationFactory getInstance() {
        return instance;
    }
    
    /**
     * Returns the invoker. This is defined by the parameter easypeas.invoker in
     * the web.xml for the servlet.<br>
     * The default is {@link InvokerImpl}
     *
     * @return the invoker
     *
     * @throws InvokerNotFoundException
     *             the invoker not found exception
     */
    public Invoker invoker() throws InvokerNotFoundException {
        String property = System.getProperty(EasyPeasConstants.EASYPEA_INVOKER);
        Invoker result = null;
        logger.finer("Property defined for:"
                + EasyPeasConstants.EASYPEA_INVOKER + " = " + property);
        if (property == null) {
            result = new InvokerImpl();
            
        } else {
            try {
                result = (Invoker) ObjectCreatorFactory.getInstance().creator()
                .create(property);
            } catch (Exception e) {
                
                logger.log(Level.WARNING,"Error creating Invoker for class:" + property, e);
                
                throw new InvokerNotFoundException(
                        "Error creating Invoker for class:" + property,
                        new InvokerImpl());
            }
        }
        return result;
    }
    
}
